package com.yu.controller;

import cn.hutool.core.util.StrUtil;
import com.yu.entity.StuInfo;
import com.yu.entity.User;
import com.yu.exception.BusinessException;
import com.yu.service.StuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StuInfoService stuInfoService;
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            throw new BusinessException(Code.BUSINESS_ERR,"用户名或密码为空");
        }
        StuInfo one = stuInfoService.selectOne(user);
        Integer code = one != null ? Code.GET_OK : Code.GET_ERR;
        String msg = one != null ? "登陆成功" : "登陆失败";
        return new Result(code,one,msg);
    }
    @PutMapping
    public Result updateStudentInfo(@RequestBody StuInfo stuInfo) {
        Boolean flag = stuInfoService.update(stuInfo);
        String msg = flag ? "修改学生信息成功" : "学生信息修改失败，请重试！";
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, msg);
    }

    @GetMapping( "/{id}")
    public Result selectById(@PathVariable Integer id) {
        StuInfo stuInfo = stuInfoService.selectById(id);
        Integer code = stuInfo!=null ? Code.GET_OK : Code.GET_ERR;
        String msg = stuInfo!=null ? "成功获取学生信息" : "获取学生信息失败";
        return new Result(code,stuInfo,msg);
    }
    @PutMapping("/{id}")
    public Result handleStuSign(@PathVariable Integer id) {
        Boolean flag = stuInfoService.handleStuSign(id);
        Integer code = flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = flag ? "成功修改学生签到状态为是" : "修改学生签到状态失败";
        return new Result(code,flag,msg);
    }
}
